#include <dbg.h>
#include <gtest/gtest.h>

using std::string;
using std::unordered_map;

class Solution {
public:
    static int lengthOfLongestSubstring(const string& s) {
        int res = 0;
        int left = 0;
        unordered_map<char, int> hash;
        for (int right = 0; right < s.size(); ++right) {
            ++hash[s[right]];             // 右端点右移
            while (hash[s[right]] > 1) {  // 出现重复字符
                --hash[s[left]];          // 左端点右移
                ++left;
            }
            res = std::max(res, right - left + 1);  // 更新答案
        }
        return res;
    }
};

TEST(case1, lengthOfLongestSubstring) {
    const string s1 = "abcabcbb";
    EXPECT_EQ(Solution::lengthOfLongestSubstring(s1), 3);
}

TEST(case2, lengthOfLongestSubstring) {
    const string s2 = "bbbbb";
    EXPECT_EQ(Solution::lengthOfLongestSubstring(s2), 1);
}

TEST(case3, lengthOfLongestSubstring) {
    const string s3 = "pwwkew";
    EXPECT_EQ(Solution::lengthOfLongestSubstring(s3), 3);
}

TEST(case4, lengthOfLongestSubstring) {
    const string s4 = "abba";
    EXPECT_EQ(Solution::lengthOfLongestSubstring(s4), 2);
}

int main(int argc, char** argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}